﻿/* Autoduel Game
 * CS4911 Senior Design Project
 * Fall 2010 Semester
 * Ian Axelrod, Liz Ha, Andrew Muldowney, Edward Futch
 */

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AutoduelClient.GameUI
{
    /// <summary>
    /// Animated Sprite class taken (and refactored) from: http://www.royalexander.wordpress.com/2008/10/09/xna-30-and-winforms-the-easy-way
    /// </summary>
    public class AnimatedSprite
    {
		#region Fields (12)
		Texture2D t2dTexture;

		bool bAnimating = false;

		int iCurrentFrame = 0;
		int iFrameCount = 1;
		int iFrameHeight = 32;
		int iFrameOffsetX = 0;
		int iFrameOffsetY = 0;
		int iFrameWidth = 32;
		int iScreenX = 0;
		int iScreenY = 0;

		float fElapsed = 0.0f;
		float fFrameRate = 0.02f;
		#endregion

		#region Properties (5)
		public int Frame
        {
            get { return iCurrentFrame; }
            set { iCurrentFrame = (int)MathHelper.Clamp(value, 0, iFrameCount); } //sets Frame value to be between 0 and iFrameCount
        }
		public float FrameLength
        {
            get { return fFrameRate; }
            set { fFrameRate = (float)Math.Max(fFrameRate, 0f); } //ensures fFrameRate is not negative number
        }
		public bool IsAnimating
        {
            get { return bAnimating; }
            set { bAnimating = value; }
        }
		public int X
        {
            get { return iScreenX; }
            set { iScreenX = value; } //value passed in is set to iScreenX
        }
		public int Y
        {
            get { return iScreenY; }
            set { iScreenY = value; }
        }
		#endregion

		#region Constructors (1)
		// constructor
        public AnimatedSprite(
            Texture2D texture,
            int FrameOffsetX,
            int FrameOffsetY,
            int FrameWidth,
            int FrameHeight,
            int FrameCount)
        {
            t2dTexture = texture;
            iFrameOffsetX = FrameOffsetX;
            iFrameOffsetY = FrameOffsetY;
            iFrameWidth = FrameWidth;
            iFrameHeight = FrameHeight;
            iFrameCount = FrameCount;
        }
		#endregion

		#region Methods (4)

		public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset)
        {
            Draw(spriteBatch, XOffset, YOffset, true);
        }

		public void Draw(
            SpriteBatch spriteBatch,
            int XOffset,
            int YOffset,
            bool NeedBeginEnd)
        {
            if (NeedBeginEnd)
                spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            spriteBatch.Draw(
                t2dTexture,
                new Rectangle(
                  iScreenX + XOffset,
                  iScreenY + YOffset,
                  iFrameWidth,
                  iFrameHeight),
                GetSourceRect(),
                Color.White);

            if (NeedBeginEnd)
                spriteBatch.End();

        }

		// determines where on the sprite sheet we will draw sprite
        public Rectangle GetSourceRect()
        {
            return new Rectangle(
            iFrameOffsetX + (iFrameWidth * iCurrentFrame),
            iFrameOffsetY,
            iFrameWidth,
            iFrameHeight);
        }

		public void Update(GameTime gametime)
        {
            if (bAnimating)
            {
                // Accumulate elapsed time...
                fElapsed += (float)gametime.ElapsedGameTime.TotalSeconds;

                // Until it passes our frame length
                if (fElapsed > fFrameRate)
                {
                    // Increment the current frame, wrapping back to 0 at iFrameCount
                    iCurrentFrame = (iCurrentFrame + 1) % iFrameCount;

                    // Reset the elapsed frame time.
                    fElapsed = 0.0f;
                }
            }
        }

		#endregion
    }
}
